package vn.allmobile.ambnews.activities.category;

import java.util.List;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.customview.FlipViewInterface;
import vn.allmobile.ambnews.data.Category;
import vn.allmobile.ambnews.network.LoadDataListener;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.network.NetworkChangeReceiver;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.ImageLoadOption;
import vn.allmobile.ambnews.utils.Log;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;


public class CategoryFlipViewAdapter extends BaseAdapter {
	FlipViewInterface _flipview;
	//FlipView _flipviewController;
	private List<Category> _category;
	LoadDataListener _listenner;	
	
	Activity _mActivity;
	Boolean _isUserPaper;
	String _navigationTitle = "";
	CategoryFlipViewAdapter(Activity act,List<Category> category,LoadDataListener listenner, Boolean isUserPaper)
	{	_category = category;
		_listenner = listenner;
		_mActivity 	= act;
		_isUserPaper = isUserPaper;	
		if(_isUserPaper)
		{
			_navigationTitle = NetworkAPI._userListPapers.get(NetworkAPI._currentPaperIndex).getTitle();
		}
		else
		{
			_navigationTitle = NetworkAPI._papers.get(NetworkAPI._currentPaperIndex).getTitle();
		}
		
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (int) Math.ceil((float)_category.size()/Contants.NUM_ITEM_GRIDVIEW );
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see android.widget.BaseAdapter#getViewTypeCount()
	 */
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final Context context = parent.getContext();	
		View localView = convertView;
		ViewHolder localViewHolder;
		if(localView == null)
		{
			localView = LayoutInflater.from(context).inflate(R.layout.grid_layout, parent,false);
			localViewHolder = new ViewHolder(); 
			
			localViewHolder.square_txt_name = (TextView)localView.findViewById(R.id.square_txt_name);
			
			localViewHolder.txt_square[0] = (TextView)localView.findViewById(R.id.txt_square01);
			localViewHolder.img_square[0] = (ImageView)localView.findViewById(R.id.img_square01);
			localViewHolder.shadow_square[0] = (ImageView)localView.findViewById(R.id.shadow_square01);
			localViewHolder.loading[0] = (View)localView.findViewById(R.id.loading_01);
			localViewHolder.txt_square[1] = (TextView)localView.findViewById(R.id.txt_square02);
			localViewHolder.img_square[1] = (ImageView)localView.findViewById(R.id.img_square02);
			localViewHolder.shadow_square[1] = (ImageView)localView.findViewById(R.id.shadow_square02);
			localViewHolder.loading[1] = (View)localView.findViewById(R.id.loading_02);
			
			localViewHolder.txt_square[2] = (TextView)localView.findViewById(R.id.txt_square11);
			localViewHolder.img_square[2] = (ImageView)localView.findViewById(R.id.img_square11);
			localViewHolder.shadow_square[2] = (ImageView)localView.findViewById(R.id.shadow_square11);
			localViewHolder.loading[2] = (View)localView.findViewById(R.id.loading_11);
			localViewHolder.txt_square[3] = (TextView)localView.findViewById(R.id.txt_square12);
			localViewHolder.img_square[3] = (ImageView)localView.findViewById(R.id.img_square12);
			localViewHolder.shadow_square[3] = (ImageView)localView.findViewById(R.id.shadow_square12);
			localViewHolder.loading[3] = (View)localView.findViewById(R.id.loading_12);
			
			localViewHolder.txt_square[4] = (TextView)localView.findViewById(R.id.txt_square21);
			localViewHolder.img_square[4] = (ImageView)localView.findViewById(R.id.img_square21);
			localViewHolder.shadow_square[4] = (ImageView)localView.findViewById(R.id.shadow_square21);
			localViewHolder.loading[4] = (View)localView.findViewById(R.id.loading_21);
			localViewHolder.txt_square[5] = (TextView)localView.findViewById(R.id.txt_square22);
			localViewHolder.img_square[5] = (ImageView)localView.findViewById(R.id.img_square22);
			localViewHolder.shadow_square[5] = (ImageView)localView.findViewById(R.id.shadow_square22);
			localViewHolder.loading[5] = (View)localView.findViewById(R.id.loading_22);
			
			localViewHolder.grid_item_nointernet = (TextView)localView.findViewById(R.id.grid_item_nointernet);
			localViewHolder.grid_item_page = (TextView)localView.findViewById(R.id.grid_item_page);
			
			localViewHolder.settingButton = (ImageView)localView.findViewById(R.id.settingButton);
			localViewHolder.bookmarkButton  = (ImageView)localView.findViewById(R.id.bookmarkButton);
			localViewHolder.list_back_button  = (ImageView)localView.findViewById(R.id.list_back_button);
			localViewHolder.refreshButton  = (ImageView)localView.findViewById(R.id.refreshButton);
			
			setViewVisibility(localViewHolder, localView);
			localView.setTag(localViewHolder);
		}
		else
		{
			localViewHolder = (ViewHolder) localView.getTag();
		}
			
		setViewVisibility(localViewHolder, localView);
		localViewHolder.square_txt_name.setText(_navigationTitle);	
		localViewHolder.grid_item_page.setText("Trang " +(position + 1)+ "/"+ getCount());
		localViewHolder.list_back_button.setVisibility(View.VISIBLE);
		
		if(NetworkChangeReceiver.isConnected(true))
			localViewHolder.refreshButton.setVisibility(View.VISIBLE);		
		
		final int page = position;
		int i=0;
		try{
			for(i=0;i<6;i++)
			{
				Category category = null;
				category = _category.get(position*6 + i);
				if(category != null)
				{
					localViewHolder.shadow_square[i].setVisibility(View.VISIBLE);
					localViewHolder.txt_square[i].setVisibility(View.VISIBLE);
					localViewHolder.img_square[i].setVisibility(View.VISIBLE);
					Log.v("is updating:" + category.isUpdating());
					if(category.isUpdating())
					{
						localViewHolder.loading[i].setVisibility(View.VISIBLE);
						
					}
					else
					{
						localViewHolder.loading[i].setVisibility(View.GONE);
					}
					localViewHolder.img_square[i].setOnClickListener(new CategoryItemClickListener(_listenner,_mActivity,position*6 + i,category.getId(),_isUserPaper));
					localViewHolder.txt_square[i].setText(category.getTitle());
					String imageURL = "http://news.allmobile.vn/" + category.getImage() + "?mode=resize&width=120&height=120";
					ImageLoader.getInstance().displayImage(imageURL, localViewHolder.img_square[i],ImageLoadOption.options, new ImageLoadingListener() {
					    @Override
					    public void onLoadingStarted(String imageUri, View view) {
					    	Log.v("loading...");					    
					    }
					    @Override
					    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
					    	((ImageView) view).setImageResource(R.drawable.no_image_220x220);
					    	_flipview.refreshPage(page);
					    }
					    @Override
					    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					    	_flipview.refreshPage(page);
					    }
					    @Override
					    public void onLoadingCancelled(String imageUri, View view) {
					    	((ImageView) view).setImageResource(R.drawable.no_image_220x220);
					    }
					});
				}
				else
				{
					localViewHolder.shadow_square[i].setVisibility(View.GONE);
					localViewHolder.txt_square[i].setVisibility(View.GONE);
					localViewHolder.img_square[i].setVisibility(View.GONE);
				}
			}		
			
		}catch(Exception e){
			localViewHolder.shadow_square[i].setVisibility(View.GONE);
			localViewHolder.txt_square[i].setVisibility(View.GONE);
			localViewHolder.img_square[i].setVisibility(View.GONE);
		}
		
		
		return localView;
		
	}
	private void setViewVisibility(ViewHolder paramViewHolder, View paramView)
    {
      for(int i=0;i< 6;i++)
      {
    	  paramViewHolder.txt_square[i].setVisibility(View.GONE);
    	  paramViewHolder.img_square[i].setVisibility(View.GONE);
    	  paramViewHolder.shadow_square[i].setVisibility(View.GONE);
      }
    }
	static class ViewHolder{
		TextView square_txt_name;
		
		TextView[]  txt_square = new TextView[6];
		ImageView[] img_square = new ImageView[6];		
		ImageView[] shadow_square = new ImageView[6];
		View[] loading = new View[6];
		
		TextView grid_item_nointernet;		
		TextView grid_item_page;
		ImageView refreshButton;
		ImageView settingButton;
		ImageView bookmarkButton;
		ImageView list_back_button;
		
		
	}
	
}
